# 修复后的智慧医疗RAG系统测试脚本
# 使用正确的Base64编码数据

Write-Host "=== 智慧医疗RAG系统测试（修复版）===" -ForegroundColor Green
Write-Host "开始时间: $(Get-Date)" -ForegroundColor Cyan

# 配置
$ragApiUrl = "https://localhost:56704"

# 测试结果统计
$testResults = @{
    Total = 0
    Passed = 0
    Failed = 0
    Errors = @()
}

function Test-ApiEndpoint {
    param(
        [string]$Name,
        [string]$Url,
        [string]$Method = "GET",
        [object]$Body = $null,
        [hashtable]$Headers = @{}
    )
    
    $testResults.Total++
    Write-Host "`n测试: $Name" -ForegroundColor Yellow
    Write-Host "URL: $Url" -ForegroundColor Gray
    
    try {
        $params = @{
            Uri = $Url
            Method = $Method
            SkipCertificateCheck = $true
        }
        
        if ($Body) {
            $params.Body = $Body | ConvertTo-Json -Depth 10
            $params.ContentType = "application/json"
        }
        
        if ($Headers.Count -gt 0) {
            $params.Headers = $Headers
        }
        
        $response = Invoke-WebRequest @params
        $content = $response.Content | ConvertFrom-Json
        
        Write-Host "✓ 成功" -ForegroundColor Green
        Write-Host "状态码: $($response.StatusCode)" -ForegroundColor Gray
        
        if ($content) {
            Write-Host "响应数据: $($content | ConvertTo-Json -Depth 3)" -ForegroundColor Gray
        }
        
        $testResults.Passed++
        return $content
    }
    catch {
        Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
        $testResults.Failed++
        $testResults.Errors += "$Name`: $($_.Exception.Message)"
        return $null
    }
}

# 1. 测试基本端点
Write-Host "`n=== 1. 基本端点测试 ===" -ForegroundColor Magenta

# 1.1 测试主页
Test-ApiEndpoint -Name "RAG系统主页" -Url "$ragApiUrl/Home"

# 1.2 测试健康检查
Test-ApiEndpoint -Name "RAG系统健康检查" -Url "$ragApiUrl/api/Triage/health"

# 1.3 测试测试端点
Test-ApiEndpoint -Name "RAG系统测试端点" -Url "$ragApiUrl/api/Triage/test"

# 2. 测试简化分诊分析（不包含Base64数据）
Write-Host "`n=== 2. 简化分诊分析测试 ===" -ForegroundColor Magenta

# 2.1 简单胸痛测试
$simpleChestPainData = @{
    chiefComplaint = "胸痛"
    symptoms = @("胸痛", "胸闷")
    vitalSigns = @{
        bloodPressure = "140/90"
        heartRate = 100
        temperature = 37.0
        respiratoryRate = 20
        oxygenSaturation = 95
    }
    medicalHistory = "无特殊病史"
    textData = "患者主诉胸痛"
}

Test-ApiEndpoint -Name "简化分诊分析-胸痛" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $simpleChestPainData

# 2.2 简单头痛测试
$simpleHeadacheData = @{
    chiefComplaint = "头痛"
    symptoms = @("头痛", "恶心")
    vitalSigns = @{
        bloodPressure = "120/80"
        heartRate = 75
        temperature = 36.8
        respiratoryRate = 18
        oxygenSaturation = 98
    }
    medicalHistory = "无特殊病史"
    textData = "患者主诉头痛"
}

Test-ApiEndpoint -Name "简化分诊分析-头痛" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $simpleHeadacheData

# 3. 测试完整分诊分析（包含正确的Base64数据）
Write-Host "`n=== 3. 完整分诊分析测试 ===" -ForegroundColor Magenta

# 3.1 胸痛患者完整测试
$chestPainData = @{
    chiefComplaint = "剧烈胸痛，伴有胸闷和呼吸困难"
    symptoms = @("胸痛", "胸闷", "呼吸困难", "心悸", "出汗", "恶心")
    vitalSigns = @{
        bloodPressure = "160/100"
        heartRate = 120
        temperature = 37.2
        respiratoryRate = 24
        oxygenSaturation = 92
    }
    medicalHistory = "高血压病史5年，糖尿病病史3年"
    textData = "患者男性，65岁，主诉胸痛2小时，疼痛呈压榨性，向左臂放射，伴有出汗和恶心"
    voiceData = "UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmGgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
    imageData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
    physiologicalData = @{
        type = "心电图"
        values = @(0.1, 0.2, 0.15, 0.3, 0.25)
        timestamps = @("2025-01-15T10:00:00Z", "2025-01-15T10:01:00Z", "2025-01-15T10:02:00Z", "2025-01-15T10:03:00Z", "2025-01-15T10:04:00Z")
        unit = "mV"
    }
}

Test-ApiEndpoint -Name "完整分诊分析-胸痛患者" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $chestPainData

# 3.2 头痛患者完整测试
$headacheData = @{
    chiefComplaint = "突发剧烈头痛，伴有右侧肢体无力"
    symptoms = @("头痛", "右侧肢体无力", "言语不清", "恶心", "呕吐", "意识模糊")
    vitalSigns = @{
        bloodPressure = "180/110"
        heartRate = 85
        temperature = 36.8
        respiratoryRate = 18
        oxygenSaturation = 96
    }
    medicalHistory = "高血压病史8年，房颤病史2年"
    textData = "患者女性，72岁，突发剧烈头痛，右侧肢体无力，言语不清，发病时间约30分钟"
    voiceData = "UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmGgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
    imageData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
    physiologicalData = @{
        type = "血压监测"
        values = @(180, 175, 185, 170, 180)
        timestamps = @("2025-01-15T09:30:00Z", "2025-01-15T09:35:00Z", "2025-01-15T09:40:00Z", "2025-01-15T09:45:00Z", "2025-01-15T09:50:00Z")
        unit = "mmHg"
    }
}

Test-ApiEndpoint -Name "完整分诊分析-头痛患者" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $headacheData

# 4. 测试特殊场景
Write-Host "`n=== 4. 特殊场景测试 ===" -ForegroundColor Magenta

# 4.1 测试无效数据
$invalidData = @{
    chiefComplaint = ""
    symptoms = @()
}

Test-ApiEndpoint -Name "分诊分析-无效数据" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $invalidData

# 4.2 测试最小有效数据
$minimalData = @{
    chiefComplaint = "胸痛"
    symptoms = @("胸痛")
}

Test-ApiEndpoint -Name "分诊分析-最小数据" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $minimalData

# 4.3 测试不存在的端点
Test-ApiEndpoint -Name "不存在的端点" -Url "$ragApiUrl/api/NonExistent"

# 5. 性能测试
Write-Host "`n=== 5. 性能测试 ===" -ForegroundColor Magenta

$performanceData = @{
    chiefComplaint = "发热，伴有咳嗽和咽痛"
    symptoms = @("发热", "咳嗽", "咽痛", "流涕", "乏力")
    vitalSigns = @{
        bloodPressure = "120/75"
        heartRate = 85
        temperature = 38.8
        respiratoryRate = 18
        oxygenSaturation = 98
    }
    medicalHistory = "无特殊病史"
    textData = "患者男性，28岁，发热2天，伴有咳嗽、咽痛和流涕，无其他不适"
}

$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
Test-ApiEndpoint -Name "性能测试-分诊分析" -Url "$ragApiUrl/api/Triage/analyze" -Method "POST" -Body $performanceData
$stopwatch.Stop()
Write-Host "响应时间: $($stopwatch.ElapsedMilliseconds)ms" -ForegroundColor Cyan

# 6. 测试结果汇总
Write-Host "`n=== 测试结果汇总 ===" -ForegroundColor Green
Write-Host "总测试数: $($testResults.Total)" -ForegroundColor White
Write-Host "通过: $($testResults.Passed)" -ForegroundColor Green
Write-Host "失败: $($testResults.Failed)" -ForegroundColor Red
Write-Host "成功率: $([math]::Round(($testResults.Passed / $testResults.Total) * 100, 2))%" -ForegroundColor Cyan

if ($testResults.Errors.Count -gt 0) {
    Write-Host "`n错误详情:" -ForegroundColor Red
    foreach ($error in $testResults.Errors) {
        Write-Host "- $error" -ForegroundColor Red
    }
}

Write-Host "`n=== 测试完成 ===" -ForegroundColor Green
Write-Host "结束时间: $(Get-Date)" -ForegroundColor Cyan

# 保存测试结果到文件
$testReport = @{
    TestDate = Get-Date
    TotalTests = $testResults.Total
    PassedTests = $testResults.Passed
    FailedTests = $testResults.Failed
    SuccessRate = [math]::Round(($testResults.Passed / $testResults.Total) * 100, 2)
    Errors = $testResults.Errors
}

$testReport | ConvertTo-Json -Depth 10 | Out-File -FilePath "rag-test-report-$(Get-Date -Format 'yyyyMMdd-HHmmss').json" -Encoding UTF8
Write-Host "测试报告已保存到: rag-test-report-$(Get-Date -Format 'yyyyMMdd-HHmmss').json" -ForegroundColor Cyan

Write-Host "`n=== 使用说明 ===" -ForegroundColor Yellow
Write-Host "1. 确保RAG系统API正在运行 (https://localhost:56704)" -ForegroundColor White
Write-Host "2. 如果遇到SSL证书问题，脚本已配置跳过证书验证" -ForegroundColor White
Write-Host "3. 修复后的测试数据使用了正确的Base64编码" -ForegroundColor White
Write-Host "4. 建议先运行简化测试，确认基本功能正常" -ForegroundColor White 